package com.ygqh.baby.quartz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ygqh.baby.po.ScheduleJob;
import com.ygqh.baby.utils.ExceptionUtil;
import com.ygqh.baby.utils.SpringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 计划任务执行
 */
public class QuartzJobFactory implements Job {
    public final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void execute(JobExecutionContext context) {
        ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
        try {
            TaskUtils.invokMethod(scheduleJob);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = ExceptionUtil.getExceptionMsg(e);
            logger.error(e.getMessage() + " 【{}】", JSON.toJSONString(scheduleJob), e);
            try {
                Object bean = SpringUtils.getBean("scheduleJobService");
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("scheduleJob", scheduleJob);
                dataMap.put("msg", msg);
                TaskUtils.invoke(bean, bean.getClass(), "sendErrorMail", dataMap);
            } catch (Exception e1) {
                logger.error(e.getMessage(),e1);
                e1.printStackTrace();
            }
        }
        try {
            if (scheduleJob.getJobNum() != null) {
                if (scheduleJob.getJobNum() == 1) {
                    QuartzManager.deleteJob(scheduleJob);
                    scheduleJob.setJobNum(0);
                    scheduleJob.setJobStatus("0");
                } else {
                    scheduleJob.setJobNum(scheduleJob.getJobNum() - 1);
                }
                scheduleJob.setUpdateTime(new Date());
                scheduleJob.setUpdateBy("系统");
                QuartzManager.updateScheduleJob(scheduleJob);
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
            logger.error(ExceptionUtil.getExceptionMsg(e));
        }
    }

    public static void main(String[] args) {
        String json = "{\"cornValid\":false,\"createBy\":\"addUserBargain\",\"createTime\":1566997327524,\"cron\":\"07 02 04 29 08 ? 2019\",\"cronDate\":1567022527344,\"data\":\"{\\\"userBargainId\\\":19946}\",\"dataMap\":{\"userBargainId\":19946},\"description\":\"发送砍价最后一小时消息提醒\",\"ipAddr\":\"10.45.39.165\",\"isConcurrent\":\"0\",\"jobGroup\":\"Bargain\",\"jobName\":\"BargainInvitation_19946\",\"jobNum\":1,\"jobStatus\":\"1\",\"jobType\":\"DWH\",\"methodName\":\"sendBargainInvitation\",\"springId\":\"quartzJobHandler\"}";

        ScheduleJob scheduleJob = JSONObject.parseObject(json, ScheduleJob.class);
        Map<String, Object> dataMap = scheduleJob.getDataMap();
        dataMap.forEach((k,v)->{
            System.out.println(v.getClass());
        });
    }
}